package cm.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author yxh
 * @description
 * @date Created in 2025/10/23 17:10
 */
public class ListTools {
    private static final Logger log = LoggerFactory.getLogger(ListTools.class);

    public ListTools() {
    }

    public static <T> List<List<T>> splitByGroup(List<T> list, int groupCount) {
        if (list != null && !list.isEmpty() && groupCount > 0) {
            int totalSize = list.size();
            int groupSize = (int)Math.ceil((double)totalSize / (double)groupCount);
            List<List<T>> result = new ArrayList(groupCount);

            for(int i = 0; i < totalSize; i += groupSize) {
                int end = Math.min(i + groupSize, totalSize);
                result.add(list.subList(i, end));
            }

            return result;
        } else {
            return Collections.emptyList();
        }
    }

    public static <T> List<List<T>> splitBySize(List<T> list, int size) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        } else {
            int listSize = list.size();
            List<List<T>> result = new ArrayList(listSize / size + 1);
            int offset = 0;

            for(int toIdx = size; toIdx <= listSize; toIdx += size) {
                result.add(list.subList(offset, toIdx));
                offset = toIdx;
            }

            if (offset < listSize) {
                result.add(list.subList(offset, listSize));
            }

            return result;
        }
    }

    public static <E> List<E> subList(List<E> srcList, int fromIndex, int toIndex) {
        List<E> valueList = new ArrayList();
        int size = srcList.size();
        if (srcList != null && size != 0) {
            if (fromIndex > size) {
                String msg = String.format("fromIndex(%d) is bigger than size(%d)", fromIndex, size);
                log.warn(msg);
            } else if (fromIndex <= size && size < toIndex) {
                valueList = srcList.subList(fromIndex, size);
            } else if (toIndex <= size) {
                valueList = srcList.subList(fromIndex, toIndex);
            }

            return (List)valueList;
        } else {
            return (List)valueList;
        }
    }

    public static <T> T getItem(List<T> list, int index) {
        if (index >= 0 && !CollectionUtils.isEmpty(list)) {
            return list.size() > index ? list.get(index) : null;
        } else {
            return null;
        }
    }

    public static <T> void addItemIfNotNull(List<T> list, T item) {
        if (!Objects.isNull(list)) {
            if (Objects.nonNull(item)) {
                list.add(item);
            }

        }
    }

    public static <T> void addItemWithDistinct(List<T> list, T item) {
        if (!Objects.isNull(list)) {
            if (Objects.nonNull(item) && !list.contains(item)) {
                list.add(item);
            }

        }
    }

    public static <T> Boolean hasIntersection(List<T> list1, List<T> list2) {
        return CollectionUtils.isNotEmpty(CollectionUtils.intersection(list1, list2));
    }

    public static <T> Boolean noIntersection(List<T> list1, List<T> list2) {
        return CollectionUtils.isEmpty(CollectionUtils.intersection(list1, list2));
    }

    public static <T> List<T> toList(String str, String split, Class<T> cls) {
        List<T> valueList = new ArrayList();
        if (StringUtils.isBlank(str)) {
            return valueList;
        } else {
            String[] var4 = str.split(split);
            int var5 = var4.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String item = var4[var6];
                Object value;
                if (cls.getName().equals(Integer.class.getName())) {
                    value = Integer.valueOf(item);
                } else if (cls.getName().equals(Long.class.getName())) {
                    value = Long.valueOf(item);
                } else if (cls.getName().equals(Float.class.getName())) {
                    value = Float.valueOf(item);
                } else if (cls.getName().equals(Double.class.getName())) {
                    value = Double.valueOf(item);
                } else {
                    if (!cls.getName().equals(String.class.getName())) {
                        String msg = String.format("未支持的类型#class: %s, item: %s", cls.getName(), item);
                        throw new RuntimeException(msg);
                    }

                    value = String.valueOf(item);
                }

                valueList.add(value);
            }

            return valueList;
        }
    }
}
